package com.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;

import com.model.Remito;
import com.query.RemitoQuery;

public class RemitoDAOImpl extends
		BaseHibernateDAO<Remito, RemitoQuery> {

	
	@Override
	public Class<Remito> getModelClass() {
		return Remito.class;
	}

	@Override
	public List<String> getListNames() {
		List<String> list = new ArrayList<String>();
		//list.add("listaProductoCodigo");
		return list;
	}

	@Override
	public boolean existeNumeroDeRemitoDeCliente(Long numeroDeRemito, Long idDeCliente) throws Exception {
		
		boolean retorno = true;
		
		Query q = null;
		String query = "select a.id "
				+ " from OrdenDeCompra a, Remito b "
				+  "where a.id = b.ordenDeCompraId";		
		
		if (idDeCliente!=null){
			query = query + " and b.numeroRemito = :numeroRemito and a.cliente.id = :idcliente";
			q = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(query);
			q.setParameter("numeroRemito", numeroDeRemito);		
			q.setParameter("idcliente", idDeCliente);		
		}else{
			query = query + " and b.numeroRemito = :numeroRemito and a.cliente is null";
			q = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(query);
			q.setParameter("numeroRemito", numeroDeRemito);		
		}

		if (q.list().isEmpty()){
			retorno = false;
		}else{
			retorno = true;
		}
		
		return retorno;
	}
	
	
}
